package com.example.api.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

@Slf4j
public class EncryptionUtils {

    public static String sha1(String plainText) {
        try {
            // 获取SHA-1 MessageDigest实例
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            // 更新原始数据
            byte[] encodedHash = digest.digest(plainText.getBytes(StandardCharsets.UTF_8));
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : encodedHash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}
